import { rxccconfig } from './RxCCConfig';
import { Observable, EMPTY } from 'rxjs';
import { Button, isValid, warn, KeyCode, Toggle } from "cc";

export function BindToggle(toggle: Toggle): Observable<Button> {
    if (!toggle) { warn('BindToggle, toggle is null'); return EMPTY; }
    return new Observable(observer => {
        const toggleCallback = param => {
            rxccconfig.buttonWillClickCommand.execute({ node: toggle.node });
            observer.next(param);
        }
        toggle.node.on(Toggle.EventType.TOGGLE, toggleCallback);
        return () => {
            if (isValid(toggle.node)) {
                toggle.node.off(Toggle.EventType.TOGGLE, toggleCallback);
            }
        }
    })
}